<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/9/27
 * Time: 21:38
 */

namespace apps\common\components;


use mix\base\Component;

class Common extends Component
{
    /**成功返回
     * @param string $msg 消息
     * @param array $data 数据
     * @param int $code code
     * @return string
     */
    static public function success($msg = '成功', $data = array(), $code = 200)
    {
        $result = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );
        return json_encode($result);
    }

    /**失败返回
     * @param string $msg 消息
     * @param array $data 数据
     * @param int $code code
     * @return string
     */
    static public function error($msg = '失败', $data = array(), $code = 403)
    {
        $result = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );
        return json_encode($result);
    }


    /**加密
     * @param string $password 明文
     * @param string $salt
     * @return string
     */
    static public function password($password = '', $salt = '')
    {
        return md5($password . md5($password . $salt));
    }

    /**生成url
     * @param $action
     * @return string
     */
    static public function url($action)
    {
        return app()->http_host . '/' . $action;
    }

    static public function jump($url='')
    {
        return '<script>window.location.href = "' . $url . '"</script>';
    }

}